//
//  HDEasterEggsCellFactory.m
//  MCPublicModule
//
//  Created by Mirko on 16/7/5.
//  Copyright © 2016年 Mirko. All rights reserved.
//

#import "HDEasterEggsCellFactory.h"
#import "HDEasterEggsTableViewCell.h"

@interface HDEasterEggsCellFactory ()

@property (nonnull, strong) UITableView *tableView;
@property (nonnull, strong) NSMutableDictionary *models;

@end

@implementation HDEasterEggsCellFactory


-(instancetype) initWithTable:(UITableView *) tableView;
{
    self = [super init];
    if (self) {
        self.tableView = tableView;
        self.models = [NSMutableDictionary dictionary];
        [self installTableCell];
    }
    return self;
}

-(void) installTableCell
{
    UINib *nib = [UINib nibWithNibName:@"HDEasterEggsTableViewCell" bundle:[MCUtils MCPMBundle]];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"HDEasterEggsTableViewCell"];
}

-(UITableViewCell *) getTableCellWithDataSource:(id <HDEasterEggsDataSource>) dataSource index:(int) idx;
{
    HDEETableCellModel *model = [dataSource cellInfo];
    [self.models setObject:model forKey:[NSString stringWithFormat:@"%d", idx]];
    UITableViewCell *cell = nil;
    switch (model.type) {
        case kHDEasterEggsCellTypeForJumpToVC:
        case kHDEasterEggsCellTypeForDefault:
        {
            static NSString * identifier = @"kHDEasterEggsCellTypeForDefault";
            cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            [self setTableDefaultCellData:model tableViewCell:cell];
        }
            break;
        case kHDEasterEggsCellTypeForSwitchOn:
        {
            static NSString * identifier = @"HDEasterEggsTableViewCell";
            cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            [self setTableSwitchCellData:model tableViewCell:cell dataSource:dataSource];
        }
        default:
            break;
    }
    return cell;
}

-(HDEETableCellModel *) getModelWithIdx:(int) idx
{
    return [self.models objectForKey:[NSString stringWithFormat:@"%d", idx]];
}

-(UITableViewCell *) setTableDefaultCellData:(HDEETableCellModel *) model tableViewCell:(UITableViewCell *) cell
{
    cell.textLabel.text = model.cellTitle;
    return cell;
}

-(void) setTableSwitchCellData:(HDEETableCellModel *) model tableViewCell:(UITableViewCell *) cell dataSource:(id <HDEasterEggsDataSource>) dataSource;
{
    HDEETableCellModel *cellModel = [HDEETableCellModel model];
    cellModel.cellTitle = model.cellTitle;
    model.switchOn = [dataSource switchDefaultStatus];
    model.switchTitle = [dataSource switchTitle:model.switchOn];
}

@end
